/*
 #
 #  Files       : Functors.h
 #                ( C++ header file )
 #
 #  Description : The SmallMatrix Library
 #                ( http://code.google.com/p/smallmatrix )
 #
 #  Copyright   : Olivier Juan
 #                ( http://www.mas.ecp.fr/vision/Personnel/juan/ )
 #
 #  License     : CeCILL-C
 #                ( http://www.cecill.info/licences/Licence_CeCILL-C_V1-en.html )
 #
 #  This software is governed by the CeCILL-C license under French law and
 #  abiding by the rules of distribution of free software.  You can  use,
 #  modify and or redistribute the software under the terms of the CeCILL-C
 #  license as circulated by CEA, CNRS and INRIA at the following URL
 #  "http://www.cecill.info".
 #
 #  As a counterpart to the access to the source code and  rights to copy,
 #  modify and redistribute granted by the license, users are provided only
 #  with a limited warranty  and the software's author,  the holder of the
 #  economic rights,  and the successive licensors  have only  limited
 #  liability.
 #
 #  In this respect, the user's attention is drawn to the risks associated
 #  with loading,  using,  modifying and/or developing or reproducing the
 #  software by the user in light of its specific status of free software,
 #  that may mean  that it is complicated to manipulate,  and  that  also
 #  therefore means  that it is reserved for developers  and  experienced
 #  professionals having in-depth computer knowledge. Users are therefore
 #  encouraged to load and test the software's suitability as regards their
 #  requirements in conditions enabling the security of their systems and/or
 #  data to be ensured and,  more generally, to use and operate it in the
 #  same conditions as regards security.
 #
 #  The fact that you are presently reading this means that you have had
 #  knowledge of the CeCILL-C license and that you accept its terms.
 #
*/

#ifndef _SMALLMATRIX_FUNCTORS_H
#define _SMALLMATRIX_FUNCTORS_H

#include <SmallMatrix/SmallMatrix.h>

NAMESPACE_BEGIN(SmallMatrix)

#define FUNCTOR_DEFAULT			0
#define FUNCTOR_ADDITIVE		1
#define FUNCTOR_MULTIPLICATIVE	2
#define FUNCTOR_ASSIGN			4

#define UNARY_FUNCTOR(name,sign) \
	struct name { \
		enum { TYPE = FUNCTOR_DEFAULT }; \
		inline static reel result(const reel& f) { \
			return sign (f); \
		} \
	};
UNARY_FUNCTOR(UnaryPlus,+)
UNARY_FUNCTOR(UnaryMinus,-)
UNARY_FUNCTOR(UnarySqrt, std::sqrt)

#define BINARY_FUNCTOR(name,sign,type) \
	struct name { \
		enum { TYPE = type }; \
		inline static reel result(const reel& f, const reel& s) { \
			return f sign (s); \
		} \
	};

BINARY_FUNCTOR(Add,+,FUNCTOR_ADDITIVE)
BINARY_FUNCTOR(Substract,-,FUNCTOR_ADDITIVE)
//BINARY_FUNCTOR(Multiply,*,FUNCTOR_MULTIPLICATIVE)
BINARY_FUNCTOR(Mult,*,FUNCTOR_ADDITIVE)
BINARY_FUNCTOR(Div,/,FUNCTOR_ADDITIVE)

#define BINARY_FUNCTOR_ASSIGN(name,sign,type) \
	struct name { \
		enum { TYPE = type | FUNCTOR_ASSIGN }; \
		inline static reel& result(reel& f, const reel& s) { \
			return f sign (s); \
		} \
	};

BINARY_FUNCTOR_ASSIGN(AddAssign,+=,FUNCTOR_ADDITIVE)
BINARY_FUNCTOR_ASSIGN(SubstractAssign,-=,FUNCTOR_ADDITIVE)
BINARY_FUNCTOR_ASSIGN(MultAssign,*=,FUNCTOR_ADDITIVE)
BINARY_FUNCTOR_ASSIGN(DivAssign,/=,FUNCTOR_ADDITIVE)
BINARY_FUNCTOR_ASSIGN(SqrtAssign, = ::sqrt ,FUNCTOR_ADDITIVE)
BINARY_FUNCTOR_ASSIGN(Assign,=,FUNCTOR_DEFAULT)

NAMESPACE_END

#endif
